import { usePublicHooks } from "../../../hooks";
import { usePublicFormats } from "../../../formats";

import { reactive, onMounted, h, toRaw } from "vue";
import { delay } from "@pureadmin/utils";
import { MemberList } from "@/api/dingtalk/member";
import { addDialog } from "@/components/ReDialog";
import { message } from "@/utils/message";
import type { FormItemProps } from "./types";

export function dingtalkMemberConfig() {
  const form = reactive({
    account_type: 1,
    page: 1,
    limit: 20
  });

  //从父级钩子里取东西
  const { pagination, loadingConfig, dataList, formRef, loading } =
    usePublicHooks();

  const { orderStatusShow, orderTypeShow } = usePublicFormats();

  const columns: TableColumnList = [
    {
      label: "手机号",
      prop: "mobile",
    },
    {
      label: "绑定车牌",
      prop: "cars",
    },
    {
      label: "可用余额",
      prop: "money",
    },
    {
      label: "状态",
      prop: "state",
      cellRenderer: ({ row }) => {
        let state: string;
        switch (row.state) {
          case 1:
            state = "正常";
            break;
          case 2:
            state = "禁用";
            break;
          default:
            state = "过期";
            break;
        }
        return h("span", { style: { state } }, state);
      }
    },

    {
      label: "备注",
      prop: "remarks",
    },

  ];

  async function onSearch() {
    loading.value = true;
    const { data } = await MemberList(toRaw(form));
    dataList.value = data.list;
    pagination.total = data.total;
    pagination.pageSize = data.pageSize;
    pagination.currentPage = data.currentPage;

    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  //页面加载
  onMounted(() => {
    onSearch();
  });

  //页面条数变化
  function onSizeChange(val) {
    form.limit = val;
    onSearch();
  }

  //页码变化
  function onCurrentChange(val) {
    loadingConfig.text = `正在加载第${val}页...`;
    loading.value = true;
    delay(600).then(() => {
      form.page = val;
      onSearch();

      //刷新数据
      loading.value = false;
    });
  }

  //重置表单
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }


  return {
    columns,
    form,
    loading,
    onSearch,
    resetForm,
    dataList,
    pagination,
    onSizeChange,
    onCurrentChange,
    loadingConfig,
  };
}
